package com.ruoyi.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.system.domain.bo.HnWorkOrderActionBo;
import com.ruoyi.system.domain.vo.HnWorkOrderActionVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.system.domain.vo.HnWorkOrderVo;
import com.ruoyi.system.domain.bo.HnWorkOrderBo;
import com.ruoyi.system.service.IHnWorkOrderService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 工单对象控制器
 * 前端访问路由地址为:/system/workOrder
 *
 * @author ruoyi
 * @date 2024-04-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workOrder")
public class HnWorkOrderController extends BaseController {

    private final IHnWorkOrderService iHnWorkOrderService;

    /**
     * 查询工单对象列表
     */
    @SaCheckPermission("system:workOrder:list")
    @GetMapping("/list")
    public TableDataInfo<HnWorkOrderVo> list(HnWorkOrderBo bo, PageQuery pageQuery) {
        return iHnWorkOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 处理工单
     *
     * @param bo 入参实体类
     * @return {@link R}<{@link HnWorkOrderActionVo}>
     */
    @SaCheckPermission("system:workOrder:completeCheck")
    @Log(title = "工单处理", businessType = BusinessType.UPDATE)
    @PostMapping("/processing")
    public R<HnWorkOrderActionVo> processing(@Validated(AddGroup.class) @RequestBody HnWorkOrderActionBo bo){
        return R.ok(iHnWorkOrderService.processing(bo));
    }

    /**
     * 导出工单对象列表
     */
    @SaCheckPermission("system:workOrder:export")
    @Log(title = "工单对象", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HnWorkOrderBo bo, HttpServletResponse response) {
        List<HnWorkOrderVo> list = iHnWorkOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "工单对象", HnWorkOrderVo.class, response);
    }

    /**
     * 获取工单对象详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:workOrder:query")
    @GetMapping("/{id}")
    public R<HnWorkOrderVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iHnWorkOrderService.queryById(id));
    }

    /**
     * 新增工单对象
     */
    @SaCheckPermission("system:workOrder:add")
    @Log(title = "工单对象", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HnWorkOrderBo bo) {
        return toAjax(iHnWorkOrderService.insertByBo(bo));
    }

    /**
     * 修改工单对象
     */
    @SaCheckPermission("system:workOrder:edit")
    @Log(title = "工单对象", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HnWorkOrderBo bo) {
        return toAjax(iHnWorkOrderService.updateByBo(bo));
    }

    /**
     * 删除工单对象
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:workOrder:remove")
    @Log(title = "工单对象", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iHnWorkOrderService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
